

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="若发现文章有误，欢迎提出宝贵的建议，敬请指教，感谢@[TOC]  运行环境  Docker-ce 20.10.11 完全分布式集群 JDK8 MySQL 5.7 Hadoop3.3.0 Hive3.1.2 on MapReduce  数据说明 测试数据获取地址：点击查看 数据说明： video     字段 类型 描述    videoId String, 视频唯一ID ，11位字符串   u">
<meta property="og:type" content="article">
<meta property="og:title" content="Hive 笔记4 案例实操 _ 统计影视网站的常规指标，各种 TopN指标 _ 观看数Top10 _explode 炸裂统计视频类别的观看数 _ 嵌套子查询">
<meta property="og:url" content="http://example.com/2022/04/04/Hive%20%E7%AC%94%E8%AE%B04%20%E6%A1%88%E4%BE%8B%E5%AE%9E%E6%93%8D%20_%20%E7%BB%9F%E8%AE%A1%E5%BD%B1%E8%A7%86%E7%BD%91%E7%AB%99%E7%9A%84%E5%B8%B8%E8%A7%84%E6%8C%87%E6%A0%87%EF%BC%8C%E5%90%84%E7%A7%8D%20TopN%E6%8C%87%E6%A0%87%20_%20%E8%A7%82%E7%9C%8B%E6%95%B0Top10%20_explode%20%E7%82%B8%E8%A3%82%E7%BB%9F%E8%AE%A1%E8%A7%86%E9%A2%91%E7%B1%BB%E5%88%AB%E7%9A%84%E8%A7%82%E7%9C%8B%E6%95%B0%20_%20%E5%B5%8C%E5%A5%97%E5%AD%90%E6%9F%A5%E8%AF%A2/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="若发现文章有误，欢迎提出宝贵的建议，敬请指教，感谢@[TOC]  运行环境  Docker-ce 20.10.11 完全分布式集群 JDK8 MySQL 5.7 Hadoop3.3.0 Hive3.1.2 on MapReduce  数据说明 测试数据获取地址：点击查看 数据说明： video     字段 类型 描述    videoId String, 视频唯一ID ，11位字符串   u">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/1f2025006c1e42e795d4bf9ca6097a2e.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ed9d7e753fee439d8a8a131d2ee88f33.png">
<meta property="article:published_time" content="2022-04-04T09:12:03.000Z">
<meta property="article:modified_time" content="2022-08-22T15:44:49.708Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Hive">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/1f2025006c1e42e795d4bf9ca6097a2e.png#pic_center">
  
  
  
  <title>Hive 笔记4 案例实操 _ 统计影视网站的常规指标，各种 TopN指标 _ 观看数Top10 _explode 炸裂统计视频类别的观看数 _ 嵌套子查询 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Hive 笔记4 案例实操 _ 统计影视网站的常规指标，各种 TopN指标 _ 观看数Top10 _explode 炸裂统计视频类别的观看数 _ 嵌套子查询"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-04-04 17:12" pubdate>
          2022年4月4日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          9.6k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          80 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Hive 笔记4 案例实操 _ 统计影视网站的常规指标，各种 TopN指标 _ 观看数Top10 _explode 炸裂统计视频类别的观看数 _ 嵌套子查询</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p><img src="https://img-blog.csdnimg.cn/1f2025006c1e42e795d4bf9ca6097a2e.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<blockquote>
<p>若发现文章有误，欢迎提出宝贵的建议，敬请指教，感谢<br>@[TOC]</p>
</blockquote>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<ul>
<li>Docker-ce 20.10.11</li>
<li>完全分布式集群</li>
<li>JDK8</li>
<li>MySQL 5.7</li>
<li>Hadoop3.3.0</li>
<li>Hive3.1.2 on MapReduce</li>
</ul>
<h1 id="数据说明"><a href="#数据说明" class="headerlink" title="数据说明"></a>数据说明</h1><hr>
<p>测试数据获取地址：<a target="_blank" rel="noopener" href="https://gitee.com/ccuni/hive-learn/tree/master/Hive%E6%A1%88%E4%BE%8B%E5%AE%9E%E6%93%8D%E5%BD%B1%E8%A7%86%E7%BD%91%E7%AB%99%E5%B8%B8%E8%A7%84%E6%8C%87%E6%A0%87/data">点击查看</a></p>
<p>数据说明：</p>
<p><strong>video</strong> </p>
<table>
<thead>
<tr>
<th align="left">字段</th>
<th align="left">类型</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">videoId</td>
<td align="left">String,</td>
<td align="left">视频唯一ID ，11位字符串</td>
</tr>
<tr>
<td align="left">uploader</td>
<td align="left">String</td>
<td align="left">上传视频的用户名</td>
</tr>
<tr>
<td align="left">age</td>
<td align="left">Int</td>
<td align="left">视频在平台上的天数</td>
</tr>
<tr>
<td align="left">category</td>
<td align="left">Array&lt; String &gt;</td>
<td align="left">上传视频指定的视频分类</td>
</tr>
<tr>
<td align="left">length</td>
<td align="left">Int</td>
<td align="left">整型数字表示的视频长度，单位秒</td>
</tr>
<tr>
<td align="left">views</td>
<td align="left">Int</td>
<td align="left">视频被浏览的次数</td>
</tr>
<tr>
<td align="left">rate</td>
<td align="left">Double</td>
<td align="left">视频的评分，满分上限为5分</td>
</tr>
<tr>
<td align="left">Ratings</td>
<td align="left">Int</td>
<td align="left">视频的流量，整型数字</td>
</tr>
<tr>
<td align="left">comments</td>
<td align="left">Int</td>
<td align="left">一个视频的整数评论数</td>
</tr>
<tr>
<td align="left">relatedId</td>
<td align="left">Array&lt; String &gt;</td>
<td align="left">相关视频的ID，不超过20个</td>
</tr>
</tbody></table>
<p>数据说明：1.txt、2.txt、3.txt、4.txt、5.txt是同一种数据，数据量依次递增，内容以”\t” 制表符分割，同一个字段，比如<code>category</code>，视频分类，以”&amp;”符号分割</p>
<p><img src="https://img-blog.csdnimg.cn/ed9d7e753fee439d8a8a131d2ee88f33.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><strong>user</strong></p>
<table>
<thead>
<tr>
<th align="left">字段</th>
<th align="left">类型</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">uploader</td>
<td align="left">String</td>
<td align="left">上传者用户名</td>
</tr>
<tr>
<td align="left">videos</td>
<td align="left">Int</td>
<td align="left">上传视频数</td>
</tr>
<tr>
<td align="left">friends</td>
<td align="left">Int</td>
<td align="left">朋友数量</td>
</tr>
</tbody></table>
<p>数据说明：user.txt，大小为35, 643 KB，以”\t”制表符分割</p>
<p>在Hive中创建的表：</p>
<p>原始表：v_ori, v_user_ori<br>最终表: v_orc, v_user_orc</p>
<h1 id="一、Hive-SQL-创建测试表"><a href="#一、Hive-SQL-创建测试表" class="headerlink" title="一、Hive SQL 创建测试表"></a>一、Hive SQL 创建测试表</h1><hr>
<h2 id="1-1-原始表"><a href="#1-1-原始表" class="headerlink" title="1.1 原始表"></a>1.1 原始表</h2><p><strong>创建原始电影表 v_ori</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> v_ori (<br>    videoId string, uploader string, age <span class="hljs-type">int</span>,<br>    category <span class="hljs-keyword">array</span><span class="hljs-operator">&lt;</span>string<span class="hljs-operator">&gt;</span>, length <span class="hljs-type">int</span>,<br>    views <span class="hljs-type">int</span>, rate <span class="hljs-type">float</span>, ratings <span class="hljs-type">int</span>, comments <span class="hljs-type">int</span>,<br>    relatedId <span class="hljs-keyword">array</span><span class="hljs-operator">&lt;</span>string<span class="hljs-operator">&gt;</span>)<br><span class="hljs-type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="hljs-keyword">BY</span> &quot;\t&quot; <br>COLLECTION ITEMS TERMINATED <span class="hljs-keyword">BY</span> &quot;&amp;&quot;<br>STORED <span class="hljs-keyword">AS</span> textfile;<br></code></pre></td></tr></table></figure>

<p><strong>创建原始用户数据表: v_user_ori</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> v_user_ori(<br>    uploader string,<br>    videos <span class="hljs-type">int</span>, <br>    friends <span class="hljs-type">int</span><br>)<br><span class="hljs-type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="hljs-keyword">BY</span> &quot;\t&quot; STORED <span class="hljs-keyword">AS</span> textfile;<br></code></pre></td></tr></table></figure>
<h2 id="1-2-orc压缩表"><a href="#1-2-orc压缩表" class="headerlink" title="1.2 orc压缩表"></a>1.2 orc压缩表</h2><p><strong>创建 orc 存储格式带 snappy 压缩的表：</strong><br><strong>v_orc</strong> 电影表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> v_orc( <br>    videoId string, <br>    uploader string,<br>    age <span class="hljs-type">int</span>,<br>    category <span class="hljs-keyword">array</span><span class="hljs-operator">&lt;</span>string<span class="hljs-operator">&gt;</span>, <br>    length <span class="hljs-type">int</span>,<br>    views <span class="hljs-type">int</span>, <br>    rate <span class="hljs-type">float</span>, <br>    ratings <span class="hljs-type">int</span>, <br>    comments <span class="hljs-type">int</span>,<br>    relatedId <span class="hljs-keyword">array</span><span class="hljs-operator">&lt;</span>string<span class="hljs-operator">&gt;</span><br>) <br>STORED <span class="hljs-keyword">AS</span> orc<br>TBLPROPERTIES(&quot;orc.compress&quot;<span class="hljs-operator">=</span>&quot;SNAPPY&quot;);<br></code></pre></td></tr></table></figure>



<p><strong>v_user_orc</strong> 用户表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> v_user_orc(<br>    uploader string,<br>    videos <span class="hljs-type">int</span>, <br>    friends <span class="hljs-type">int</span><br>)<br><span class="hljs-type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="hljs-keyword">BY</span> &quot;\t&quot;<br>STORED <span class="hljs-keyword">AS</span> ORC<br>TBLPROPERTIES(&quot;orc.compress&quot;<span class="hljs-operator">=</span>&quot;SNAPPY&quot;);<br></code></pre></td></tr></table></figure>

<p>加载数据集到原始表：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">hive (uni) &gt; load data local inpath &#x27;/opt/module/hive/data/video&#x27; into table v_ori;<br><br>hive (uni) &gt; load data local inpath &#x27;/opt/module/hive/data/user&#x27; into table v_user_ori;<br></code></pre></td></tr></table></figure>
<p>插入原始表的数据到orc压缩表：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">hive (uni) &gt; insert into table v_orc select * from v_ori;<br><br>hive (uni) &gt; insert into table v_user_orc select * from v_user_ori;<br></code></pre></td></tr></table></figure>

<h1 id="二、Top指标"><a href="#二、Top指标" class="headerlink" title="二、Top指标"></a>二、Top指标</h1><hr>
<h2 id="2-1-统计观看数-Top10"><a href="#2-1-统计观看数-Top10" class="headerlink" title="2.1 统计观看数 Top10"></a>2.1 统计观看数 Top10</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> videoid<br><span class="hljs-keyword">FROM</span> v_orc<br><span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span><br>LIMIT <span class="hljs-number">10</span>;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs dns">Stage-Stage-<span class="hljs-number">1</span>: Map: <span class="hljs-number">1</span>  Reduce: <span class="hljs-number">1</span>   Cumulative CPU: <span class="hljs-number">9</span>.<span class="hljs-number">35</span> sec   HDFS Read: <span class="hljs-number">9715792</span> HDFS Write: <span class="hljs-number">417</span> SUCCESS<br>Total MapReduce CPU Time Spent: <span class="hljs-number">9</span> seconds <span class="hljs-number">350</span> msec<br>OK<br>videoid	views<br>dMH0bHeiRNg	<span class="hljs-number">42513417</span><br><span class="hljs-number">0</span>XxI-hvPRRA	<span class="hljs-number">20282464</span><br><span class="hljs-number">1d</span>mVU08zVpA	<span class="hljs-number">16087899</span><br>RB-wUgnyGv<span class="hljs-number">0	15712924</span><br>QjA5faZF1A<span class="hljs-number">8	15256922</span><br>-_CSo1gOd<span class="hljs-number">48	13199833</span><br><span class="hljs-number">49</span>IDp76kjPw	<span class="hljs-number">11970018</span><br>tYnn51C3X_w	<span class="hljs-number">11823701</span><br>pv5zWaTEVkI	<span class="hljs-number">11672017</span><br>D2kJZOfq7zk	<span class="hljs-number">11184051</span><br>Time taken: <span class="hljs-number">37</span>.<span class="hljs-number">96</span> seconds, Fetched: <span class="hljs-number">10</span> row(s)<br></code></pre></td></tr></table></figure>
<h2 id="2-2-统计最受欢迎的影视类别TOP10"><a href="#2-2-统计最受欢迎的影视类别TOP10" class="headerlink" title="2.2 统计最受欢迎的影视类别TOP10"></a>2.2 统计最受欢迎的影视类别TOP10</h2><p><strong>思路：</strong><br>1.category类别的形式为类别1,类别2,类别3…<br>可使用explode函数对其炸裂</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> explode(category)<br><span class="hljs-keyword">FROM</span> v_orc;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果（多个重复的影视类型）：</strong></p>
<figure class="highlight node-repl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs node-repl"><span class="hljs-meta prompt_">...</span><br>Politics<br>News<br>Politics<br>News<br>Politics<br></code></pre></td></tr></table></figure>
<p>2.若需要关联其他字段则使用LATERAL VIEW 连接</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> videoid, category_name<br><span class="hljs-keyword">FROM</span> v_orc<br><span class="hljs-keyword">LATERAL</span> <span class="hljs-keyword">VIEW</span> explode(category) category_tmp <span class="hljs-keyword">AS</span> category_name;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果</strong>（多个重复的影视ID和影视类别）：</p>
<figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs stata">...<br>UDZQNy_xLFk	<span class="hljs-keyword">News</span><br>UDZQNy_xLFk	Politics<br>dpCm6wTEM28	<span class="hljs-keyword">News</span><br>dpCm6wTEM28	Politics<br>XryvsJOIAKA	Music<br>WJLYuAofX3I	<span class="hljs-keyword">News</span><br>WJLYuAofX3I	Politics<br></code></pre></td></tr></table></figure>

<p><strong>最终：嵌套子查询</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> category_name, <span class="hljs-built_in">COUNT</span>(<span class="hljs-operator">*</span>) ct<br><span class="hljs-keyword">FROM</span> (<br>  <span class="hljs-keyword">SELECT</span> explode(category) category_name<br>  <span class="hljs-keyword">FROM</span> v_orc) t1<br><span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> category_name<br><span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> ct <span class="hljs-keyword">DESC</span><br>LIMIT <span class="hljs-number">10</span>;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">category_name</span>	ct<br><span class="hljs-attribute">Music</span>	<span class="hljs-number">179049</span><br><span class="hljs-attribute">Entertainment</span>	<span class="hljs-number">127674</span><br><span class="hljs-attribute">Comedy</span>	<span class="hljs-number">87818</span><br><span class="hljs-attribute">Animation</span>	<span class="hljs-number">73293</span><br><span class="hljs-attribute">Film</span>	<span class="hljs-number">73293</span><br><span class="hljs-attribute">Sports</span>	<span class="hljs-number">67329</span><br><span class="hljs-attribute">Games</span>	<span class="hljs-number">59817</span><br><span class="hljs-attribute">Gadgets</span>	<span class="hljs-number">59817</span><br><span class="hljs-attribute">People</span>	<span class="hljs-number">48890</span><br><span class="hljs-attribute">Blogs</span>	<span class="hljs-number">48890</span><br></code></pre></td></tr></table></figure>

<h2 id="2-3-统计观看数最高的视频类别TOP20以及该类别视频个数"><a href="#2-3-统计观看数最高的视频类别TOP20以及该类别视频个数" class="headerlink" title="2.3 统计观看数最高的视频类别TOP20以及该类别视频个数"></a>2.3 统计观看数最高的视频类别TOP20以及该类别视频个数</h2><p><strong>思路：</strong><br>1.求出观看数最高的视频类别TOP 20</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> category, views<br><span class="hljs-keyword">FROM</span> v_orc<br><span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span><br>LIMIT <span class="hljs-number">20</span>;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs css">category	views<br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Comedy&quot;</span>]</span>	<span class="hljs-number">42513417</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Comedy&quot;</span>]</span>	<span class="hljs-number">20282464</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Entertainment&quot;</span>]</span>	<span class="hljs-number">16087899</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Entertainment&quot;</span>]</span>	<span class="hljs-number">15712924</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Music&quot;</span>]</span>	<span class="hljs-number">15256922</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;People&quot;</span>,<span class="hljs-string">&quot;Blogs&quot;</span>]</span>	<span class="hljs-number">13199833</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Comedy&quot;</span>]</span>	<span class="hljs-number">11970018</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Music&quot;</span>]</span>	<span class="hljs-number">11823701</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Music&quot;</span>]</span>	<span class="hljs-number">11672017</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;People&quot;</span>,<span class="hljs-string">&quot;Blogs&quot;</span>]</span>	<span class="hljs-number">11184051</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Entertainment&quot;</span>]</span>	<span class="hljs-number">10786529</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Entertainment&quot;</span>]</span>	<span class="hljs-number">10334975</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Comedy&quot;</span>]</span>	<span class="hljs-number">10107491</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Music&quot;</span>]</span>	<span class="hljs-number">9579911</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Comedy&quot;</span>]</span>	<span class="hljs-number">9566609</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;UNA&quot;</span>]</span>	<span class="hljs-number">8825788</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Music&quot;</span>]</span>	<span class="hljs-number">7533070</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Entertainment&quot;</span>]</span>	<span class="hljs-number">7456875</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Comedy&quot;</span>]</span>	<span class="hljs-number">7066676</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Entertainment&quot;</span>]</span>	<span class="hljs-number">6952767</span><br><span class="hljs-selector-tag">Time</span> taken: <span class="hljs-number">36.638</span> seconds, Fetched: <span class="hljs-number">20</span> <span class="hljs-built_in">row</span>(s<br></code></pre></td></tr></table></figure>
<p><strong>2.将视频类别炸开</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> explode(category) category_name<br><span class="hljs-keyword">FROM</span> v_orc;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">News</span><br><span class="hljs-attribute">Politics</span><br><span class="hljs-attribute">News</span><br><span class="hljs-attribute">Politics</span><br><span class="hljs-attribute">Music</span><br><span class="hljs-attribute">News</span><br><span class="hljs-attribute">Politics</span><br><span class="hljs-attribute">Time</span> taken: <span class="hljs-number">0</span>.<span class="hljs-number">283</span> seconds, Fetched: <span class="hljs-number">1019206</span> row(s)<br></code></pre></td></tr></table></figure>
<p>通过结果可以得出，观看数前20的视频里共有六种类型，其中出现最多的为Politics</p>
<p><strong>最终: 嵌套子查询：</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> category_name, <span class="hljs-built_in">COUNT</span>(<span class="hljs-operator">*</span>) ct<br><span class="hljs-keyword">FROM</span> (<br>  <span class="hljs-keyword">SELECT</span> explode(category) category_name<br>  <span class="hljs-keyword">FROM</span> (<br>   <span class="hljs-keyword">SELECT</span> category, views<br>   <span class="hljs-keyword">FROM</span> v_orc<br>   <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span><br>   LIMIT <span class="hljs-number">20</span>)t1<br>  )t2<br><span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> category_name;<br></code></pre></td></tr></table></figure>

<p><strong>运行结果：</strong></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">category_name</span>	ct<br><span class="hljs-attribute">Blogs</span>	<span class="hljs-number">2</span><br><span class="hljs-attribute">Comedy</span>	<span class="hljs-number">6</span><br><span class="hljs-attribute">Entertainment</span>	<span class="hljs-number">6</span><br><span class="hljs-attribute">Music</span>	<span class="hljs-number">5</span><br><span class="hljs-attribute">People</span>	<span class="hljs-number">2</span><br><span class="hljs-attribute">UNA</span>	<span class="hljs-number">1</span><br><span class="hljs-attribute">Time</span> taken: <span class="hljs-number">105</span>.<span class="hljs-number">114</span> seconds, Fetched: <span class="hljs-number">6</span> row(s)<br></code></pre></td></tr></table></figure>

<h2 id="2-4-统计视频观看数Top10所关联视频的所属类别排序"><a href="#2-4-统计视频观看数Top10所关联视频的所属类别排序" class="headerlink" title="2.4 统计视频观看数Top10所关联视频的所属类别排序"></a>2.4 统计视频观看数Top10所关联视频的所属类别排序</h2><p>1.求出视频观看数Top10所关联的视频（数组）</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> relatedId, views<br><span class="hljs-keyword">FROM</span> v_orc <br><span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span><br>LIMIT <span class="hljs-number">10</span>;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-attr">[<span class="hljs-string">&quot;W4WdLT6_Hz0&quot;</span>,<span class="hljs-string">&quot;jK6V5GZDOe4&quot;</span>,<span class="hljs-string">&quot;xQtUycLro-s&quot;</span>,<span class="hljs-string">&quot;dOr3G1jsofs&quot;</span>,<span class="hljs-string">&quot;wdwuM8JGaHM&quot;</span>,<span class="hljs-string">&quot;70dwvafpKhM&quot;</span>,<span class="hljs-string">&quot;mvyc1AjI7UA&quot;</span>,<span class="hljs-string">&quot;YJvtmoR9EYk&quot;</span>,<span class="hljs-string">&quot;ykZtlMDc8go&quot;</span>,<span class="hljs-string">&quot;iSnQZaCuq6Y&quot;</span>,<span class="hljs-string">&quot;TIBRpeBV1tI&quot;</span>,<span class="hljs-string">&quot;_JTR00VNRPo&quot;</span>,<span class="hljs-string">&quot;kD3qx7PndCA&quot;</span>,<span class="hljs-string">&quot;KYPk6PztNHM&quot;</span>,<span class="hljs-string">&quot;4jvWyog4mWc&quot;</span>,<span class="hljs-string">&quot;yG9o7z7S_Yc&quot;</span>,<span class="hljs-string">&quot;yJDiGVTg1DY&quot;</span>,<span class="hljs-string">&quot;xngs9F89mr8&quot;</span>,<span class="hljs-string">&quot;Gi5WwiNuWBo&quot;</span>,<span class="hljs-string">&quot;JkZIU0eVwBc&quot;</span>]</span>	<span class="hljs-number">11672017</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;wQ7h-UUT2Rs&quot;</span>,<span class="hljs-string">&quot;_GpOroM0g80&quot;</span>,<span class="hljs-string">&quot;XVQNt64PxfE&quot;</span>,<span class="hljs-string">&quot;lARQ8pkhlag&quot;</span>,<span class="hljs-string">&quot;hMpJk9R-Kcc&quot;</span>,<span class="hljs-string">&quot;3asVB21DiF0&quot;</span>,<span class="hljs-string">&quot;963cwe6bcQk&quot;</span>,<span class="hljs-string">&quot;lvujNfL6A6U&quot;</span>,<span class="hljs-string">&quot;tDgJ4AXD7Vw&quot;</span>,<span class="hljs-string">&quot;CORGrzTeH2U&quot;</span>,<span class="hljs-string">&quot;h9JjwQq9ptc&quot;</span>,<span class="hljs-string">&quot;FRfKnA0yMs0&quot;</span>,<span class="hljs-string">&quot;b1rhMGmjX5k&quot;</span>,<span class="hljs-string">&quot;uLbwk1xpnYg&quot;</span>,<span class="hljs-string">&quot;pxGyriIddss&quot;</span>,<span class="hljs-string">&quot;RgihOGydAx0&quot;</span>,<span class="hljs-string">&quot;NwI9TyxHbwc&quot;</span>,<span class="hljs-string">&quot;DI2TwwxMbtU&quot;</span>,<span class="hljs-string">&quot;qeR-gX3TeJE&quot;</span>,<span class="hljs-string">&quot;r43urfOKWXQ&quot;</span>]</span>	<span class="hljs-number">11184051</span><br><span class="hljs-selector-tag">Time</span> taken: <span class="hljs-number">44.3</span> seconds, Fetched: <span class="hljs-number">10</span> <span class="hljs-built_in">row</span>(s)<br></code></pre></td></tr></table></figure>

<p><strong>2.将关联视频的ID炸开</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> EXPLODE(relatedId) related_id<br><span class="hljs-keyword">FROM</span> (<br>  <span class="hljs-keyword">SELECT</span> relatedId, views<br>  <span class="hljs-keyword">FROM</span> v_orc<br>  <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span><br>  LIMIT <span class="hljs-number">10</span>) t1;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果</strong>（视频观看数TOP10视频所有的相关视频ID）：</p>
<figure class="highlight asciidoc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs asciidoc"><span class="hljs-bullet">...</span><br><span class="hljs-bullet"></span>QoREX<span class="hljs-emphasis">_TLtZo</span><br><span class="hljs-emphasis">I-cm3GF-jX0</span><br><span class="hljs-emphasis">doIQXfJvydY</span><br><span class="hljs-emphasis">6hD3gGg9jMk</span><br><span class="hljs-emphasis">Hfbzju1FluI</span><br><span class="hljs-emphasis">vVN_</span>pLl5ngg<br>3PnoFu027hc<br>7nrpwEDvusY<br></code></pre></td></tr></table></figure>
<p>3.JOIN 原表，取出关联视频所属类别（数组）</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> g.category<br><span class="hljs-keyword">FROM</span> (<br>  <span class="hljs-keyword">SELECT</span> EXPLODE(relatedId) related_id<br>  <span class="hljs-keyword">FROM</span> (<br>    <span class="hljs-keyword">SELECT</span> relatedId, views<br>    <span class="hljs-keyword">FROM</span> v_orc<br>    <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span><br>    LIMIT <span class="hljs-number">10</span>)t1<br>  )t2<br><span class="hljs-keyword">JOIN</span> v_orc g<br><span class="hljs-keyword">ON</span> t2.related_id <span class="hljs-operator">=</span> g.videoId;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs css">...<br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Sports&quot;</span>]</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Sports&quot;</span>]</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Sports&quot;</span>]</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;People&quot;</span>,<span class="hljs-string">&quot;Blogs&quot;</span>]</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Sports&quot;</span>]</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Gadgets&quot;</span>,<span class="hljs-string">&quot;Games&quot;</span>]</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Sports&quot;</span>]</span><br><span class="hljs-selector-attr">[<span class="hljs-string">&quot;Entertainment&quot;</span>]</span><br><span class="hljs-selector-tag">Time</span> taken: <span class="hljs-number">109.835</span> seconds, Fetched: <span class="hljs-number">198</span> <span class="hljs-built_in">row</span>(s)<br></code></pre></td></tr></table></figure>
<p>4.炸裂相关联视频的类别字段</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> explode(category) category_name<br><span class="hljs-keyword">FROM</span> (<br>  <span class="hljs-keyword">SELECT</span> g.category<br>    <span class="hljs-keyword">FROM</span> (<br>      <span class="hljs-keyword">SELECT</span> EXPLODE(relatedId) related_id<br>      <span class="hljs-keyword">FROM</span> (<br>        <span class="hljs-keyword">SELECT</span> relatedId, views<br>        <span class="hljs-keyword">FROM</span> v_orc<br>        <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span><br>        LIMIT <span class="hljs-number">10</span>)t1<br>     )t2<br>   <span class="hljs-keyword">JOIN</span> v_orc g<br>   <span class="hljs-keyword">ON</span> t2.related_id <span class="hljs-operator">=</span> g.videoId)t3;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs css">...<br>Sports<br>Sports<br>Sports<br>People<br>Blogs<br>Sports<br>Gadgets<br>Games<br>Sports<br>Entertainment<br><span class="hljs-selector-tag">Time</span> taken: <span class="hljs-number">113.581</span> seconds, Fetched: <span class="hljs-number">231</span> <span class="hljs-built_in">row</span>(s)<br></code></pre></td></tr></table></figure>
<p><strong>最终：嵌套子查询</strong><br>按照类别分组，求count，并按照count排序</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> category_name, <span class="hljs-built_in">count</span>(<span class="hljs-operator">*</span>) ct<br><span class="hljs-keyword">FROM</span> () t4 <br><span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> category_name<br><span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> ct <span class="hljs-keyword">DESC</span>;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">category_name</span>	ct<br><span class="hljs-attribute">Comedy</span>	<span class="hljs-number">59</span><br><span class="hljs-attribute">Entertainment</span>	<span class="hljs-number">50</span><br><span class="hljs-attribute">Music</span>	<span class="hljs-number">47</span><br><span class="hljs-attribute">Blogs</span>	<span class="hljs-number">14</span><br><span class="hljs-attribute">People</span>	<span class="hljs-number">14</span><br><span class="hljs-attribute">Sports</span>	<span class="hljs-number">8</span><br><span class="hljs-attribute">Animation</span>	<span class="hljs-number">5</span><br><span class="hljs-attribute">Games</span>	<span class="hljs-number">5</span><br><span class="hljs-attribute">Gadgets</span>	<span class="hljs-number">5</span><br><span class="hljs-attribute">Film</span>	<span class="hljs-number">5</span><br><span class="hljs-attribute">Politics</span>	<span class="hljs-number">4</span><br><span class="hljs-attribute">News</span>	<span class="hljs-number">4</span><br><span class="hljs-attribute">Animals</span>	<span class="hljs-number">2</span><br><span class="hljs-attribute">Pets</span>	<span class="hljs-number">2</span><br><span class="hljs-attribute">Howto</span>	<span class="hljs-number">2</span><br><span class="hljs-attribute">DIY</span>	<span class="hljs-number">2</span><br><span class="hljs-attribute">Autos</span>	<span class="hljs-number">1</span><br><span class="hljs-attribute">UNA</span>	<span class="hljs-number">1</span><br><span class="hljs-attribute">Vehicles</span>	<span class="hljs-number">1</span><br><span class="hljs-attribute">Time</span> taken: <span class="hljs-number">175</span>.<span class="hljs-number">615</span> seconds, Fetched: <span class="hljs-number">19</span> row(s)<br></code></pre></td></tr></table></figure>
<h2 id="2-5-统计每个类别中的视频热度Top10，以Music为例"><a href="#2-5-统计每个类别中的视频热度Top10，以Music为例" class="headerlink" title="2.5 统计每个类别中的视频热度Top10，以Music为例"></a>2.5 统计每个类别中的视频热度Top10，以Music为例</h2><p>1.创建中间表存储视频类别炸开后的数据</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> v_orc_category( <br>    videoId string, <br>    uploader string,<br>    age <span class="hljs-type">int</span>,<br>    category string,<br>    length <span class="hljs-type">int</span>,<br>    views <span class="hljs-type">int</span>, <br>    rate <span class="hljs-type">float</span>, <br>    ratings <span class="hljs-type">int</span>, <br>    comments <span class="hljs-type">int</span>,<br>    relatedId <span class="hljs-keyword">array</span><span class="hljs-operator">&lt;</span>string<span class="hljs-operator">&gt;</span><br>) <br>STORED <span class="hljs-keyword">AS</span> orc<br>TBLPROPERTIES(&quot;orc.compress&quot;<span class="hljs-operator">=</span>&quot;SNAPPY&quot;);<br></code></pre></td></tr></table></figure>

<p>2.炸裂原数据，直接导入到中间表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-keyword">table</span> v_orc_category<br><span class="hljs-keyword">SELECT</span> <br>  videoId,<br>  uploader,<br>  age,<br>  category_name,<br>  length,<br>  views,<br>  rate,<br>  ratings,<br>  comments,<br>  relatedId<br> <span class="hljs-keyword">FROM</span> v_orc <br> <span class="hljs-keyword">LATERAL</span> <span class="hljs-keyword">VIEW</span> explode(category) category_tmp <span class="hljs-keyword">AS</span> category_name;<br></code></pre></td></tr></table></figure>

<p>3.查看中间表炸裂后的结果：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> videoId, category<br><span class="hljs-keyword">FROM</span> v_orc_category<br>LIMIT <span class="hljs-number">10</span>;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs css">videoid	category<br>LKh7zAJ4nwo	Entertainment<br><span class="hljs-number">7</span>D0Mf4Kn4Xk	Music<br>n1cEq1C8oqQ	Comedy<br>OHkEzL4Unck	Comedy<br>-boOvAGNKUc	Music<br>hFFH8DaOHQg	Music<br>LzHjIj3fpR8	Comedy<br>SDNkMu8ZT68	People<br>SDNkMu8ZT68	Blogs<br>PkGUU_ggO3k	Entertainment<br><span class="hljs-selector-tag">Time</span> taken: <span class="hljs-number">0.264</span> seconds, Fetched: <span class="hljs-number">10</span> <span class="hljs-built_in">row</span>(s)<br></code></pre></td></tr></table></figure>
<p><strong>最后直接查询Music的视频热度Top20</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span><br>    videoId,<br>    views<br><span class="hljs-keyword">FROM</span> v_orc_category<br><span class="hljs-keyword">WHERE</span> category <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;Music&#x27;</span><br><span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span><br>LIMIT <span class="hljs-number">10</span>;<br></code></pre></td></tr></table></figure>

<p><strong>运行结果：</strong></p>
<figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs dns">videoid	views<br>QjA5faZF1A<span class="hljs-number">8	15256922</span><br>tYnn51C3X_w	<span class="hljs-number">11823701</span><br>pv5zWaTEVkI	<span class="hljs-number">11672017</span><br><span class="hljs-number">8</span>bbTtPL1jRs	<span class="hljs-number">9579911</span><br>UMf40daefsI	<span class="hljs-number">7533070</span><br>-xEzGIuY7kw	<span class="hljs-number">6946033</span><br>d6C0bNDqf3Y	<span class="hljs-number">6935578</span><br>HSoVKUVOnfQ	<span class="hljs-number">6193057</span><br><span class="hljs-number">3</span>URfWTEPmtE	<span class="hljs-number">5581171</span><br>thtmaZnxk_<span class="hljs-number">0	5142238</span><br></code></pre></td></tr></table></figure>

<h2 id="2-6-统计每个类别视频观看数Top3"><a href="#2-6-统计每个类别视频观看数Top3" class="headerlink" title="2.6 统计每个类别视频观看数Top3"></a>2.6 统计每个类别视频观看数Top3</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span><br>    category,<br>    videoId,<br>    views<br><span class="hljs-keyword">FROM</span> (<br>    <span class="hljs-comment">-- 求出每个类别视频的观看数排名</span><br>    <span class="hljs-keyword">SELECT</span> <br>    category,<br>    videoId,<br>    views,<br>    <span class="hljs-built_in">rank</span>() <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> category <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span>) rk<br>    <span class="hljs-keyword">FROM</span> v_orc_category) t1<br><span class="hljs-keyword">WHERE</span> rk <span class="hljs-operator">&lt;=</span> <span class="hljs-number">3</span>;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">Sports</span>	<span class="hljs-number">7</span>vL19q8yL54	<span class="hljs-number">2527713</span><br><span class="hljs-attribute">Travel</span>	bNF_P281Uu4	<span class="hljs-number">5231539</span><br><span class="hljs-attribute">Travel</span>	s5ipz_0uC_U	<span class="hljs-number">1198840</span><br><span class="hljs-attribute">Travel</span>	<span class="hljs-number">6</span>jJW7aSNCzU	<span class="hljs-number">1143287</span><br><span class="hljs-attribute">UNA</span>	aRNzWyD7C9o	<span class="hljs-number">8825788</span><br><span class="hljs-attribute">UNA</span>	jtExxsiLgPM	<span class="hljs-number">5320895</span><br><span class="hljs-attribute">UNA</span>	PxNNR4symuE	<span class="hljs-number">4033376</span><br><span class="hljs-attribute">Vehicles</span>	RjrEQaG5jPM	<span class="hljs-number">2803140</span><br><span class="hljs-attribute">Vehicles</span>	cv157ZIInUk	<span class="hljs-number">2773979</span><br><span class="hljs-attribute">Vehicles</span>	Gyg9U1YaVk8	<span class="hljs-number">1832224</span><br><span class="hljs-attribute">Time</span> taken: <span class="hljs-number">38</span>.<span class="hljs-number">469</span> seconds, Fetched: <span class="hljs-number">63</span> row(s)<br></code></pre></td></tr></table></figure>
<h2 id="2-7-统计上传视频最多的用户Top10以及他们上传的视频观看次数在前20的视频"><a href="#2-7-统计上传视频最多的用户Top10以及他们上传的视频观看次数在前20的视频" class="headerlink" title="2.7 统计上传视频最多的用户Top10以及他们上传的视频观看次数在前20的视频"></a>2.7 统计上传视频最多的用户Top10以及他们上传的视频观看次数在前20的视频</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span><br>    uploader,<br>    videoId,<br>    views<br><span class="hljs-keyword">FROM</span><br>     <span class="hljs-comment">--3. 根据观看次数，对用户所上传的视频进行排名</span><br>    (<span class="hljs-keyword">SELECT</span><br>        uploader,<br>        videoId,<br>        views,<br>        <span class="hljs-built_in">rank</span>() <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">PARTITION</span> <span class="hljs-keyword">BY</span> uploader <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> views <span class="hljs-keyword">DESC</span>) rk<br>    <span class="hljs-keyword">FROM</span><br>        <span class="hljs-comment">-- 2. 跟视频表JOIN, 取出前10用户所上传的所有视频</span><br>        (<span class="hljs-keyword">SELECT</span><br>           t1.uploader,<br>           videoId,<br>           views<br>         <span class="hljs-keyword">FROM</span> <br>            <span class="hljs-comment">-- 1. 求出上传视频最多的10个用户</span><br>            (<span class="hljs-keyword">SELECT</span> uploader, videos<br>            <span class="hljs-keyword">FROM</span> v_user_orc<br>            <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> videos <span class="hljs-keyword">DESC</span><br>            LIMIT <span class="hljs-number">10</span>) t1<br>        <span class="hljs-keyword">JOIN</span> v_orc g<br>        <span class="hljs-keyword">ON</span> t1.uploader <span class="hljs-operator">=</span> g.uploader) t2<br>   ) t3<br><span class="hljs-keyword">WHERE</span> rk <span class="hljs-operator">&lt;=</span> <span class="hljs-number">20</span>;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs dns">...<br>expertvillage	foATQY3wovI	<span class="hljs-number">13576</span><br>expertvillage	-UnQ8rcBOQs	<span class="hljs-number">13450</span><br>expertvillage	crtNd46CDks	<span class="hljs-number">11639</span><br>expertvillage	D1leA0JKHhE	<span class="hljs-number">11553</span><br>expertvillage	NJu2oG1Wm<span class="hljs-number">98	11452</span><br>expertvillage	CapbXdyv<span class="hljs-number">4j4	10915</span><br>expertvillage	epr5erraEp<span class="hljs-number">4	10817</span><br>expertvillage	IyQoDgaLM7U	<span class="hljs-number">10597</span><br>expertvillage	tbZibBnusLQ	<span class="hljs-number">10402</span><br>expertvillage	_GnCHodc7mk	<span class="hljs-number">9422</span><br>expertvillage	hvEYlSlRitU	<span class="hljs-number">7123</span><br>Time taken: <span class="hljs-number">111.613</span> seconds, Fetched: <span class="hljs-number">40</span> row(s)<br></code></pre></td></tr></table></figure>

<h1 id="三、总-结"><a href="#三、总-结" class="headerlink" title="三、总 结"></a>三、总 结</h1><hr>
<p>在使用Hive写SQL语句时，需注意over窗口函数的使用，里面包含partition分区概念，groupby 分组概念。还有<code>lateral view</code>的用法需要多熟悉熟悉。</p>
<p>在处理业务时，先写简单的SQL语句，一般用嵌套子查询或者JOIN进行表关联。</p>
<p>这里只用到了MR计算引擎，其实Tez引擎和Spark引擎都比MR快得多，这里由于在配置时遇到了BUG就没有切换。留个坑，之后再试试用不同的引擎对查询的性能进行简单的比较。</p>
<h1 id="参-考-资-料"><a href="#参-考-资-料" class="headerlink" title="参  考  资  料"></a>参  考  资  料</h1><hr>
<p>[1] <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1EZ4y1G7iL?p=116">视频链接</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93/" class="category-chain-item">数据仓库</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Hive/">#Hive</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Hive 笔记4 案例实操 _ 统计影视网站的常规指标，各种 TopN指标 _ 观看数Top10 _explode 炸裂统计视频类别的观看数 _ 嵌套子查询</div>
      <div>http://example.com/2022/04/04/Hive 笔记4 案例实操 _ 统计影视网站的常规指标，各种 TopN指标 _ 观看数Top10 _explode 炸裂统计视频类别的观看数 _ 嵌套子查询/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年4月4日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/04/05/Docker%20%E7%AC%94%E8%AE%B02%20MySQL%E4%B8%AD%E7%BB%A7%E6%97%A5%E5%BF%97%E3%80%81%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E3%80%81%E4%B8%BB%E4%BB%8E%E6%9E%B6%E6%9E%84%E6%A6%82%E5%BF%B5%20_%20%E4%BD%BF%E7%94%A8Docker%E5%88%9B%E5%BB%BA%E3%80%81%E9%85%8D%E7%BD%AE%E4%B8%8E%E4%BD%BF%E7%94%A8%E4%B8%A4%E4%B8%AAMySQL%E4%B8%BB%E4%BB%8E%E5%BA%93%E5%AE%B9%E5%99%A8/" title="Docker 笔记2 MySQL中继日志、读写分离、主从架构概念 _ 使用Docker创建、配置与使用两个MySQL主从库容器">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Docker 笔记2 MySQL中继日志、读写分离、主从架构概念 _ 使用Docker创建、配置与使用两个MySQL主从库容器</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/04/04/Docker%20%E7%AC%94%E8%AE%B01%20Volumes%20%E5%8D%B7%E7%9A%84%E4%BD%9C%E7%94%A8%E4%B8%8E%E6%A1%88%E4%BE%8B%20_%20Docker%E5%AE%89%E8%A3%85%E5%B8%B8%E8%A7%81%E6%9C%8D%E5%8A%A1%20_%20Tomcat%20&amp;%20MySQL/" title="Docker 笔记1 Volumes 卷的作用与案例 _ Docker安装常见服务 _ Tomcat &amp; MySQL">
                        <span class="hidden-mobile">Docker 笔记1 Volumes 卷的作用与案例 _ Docker安装常见服务 _ Tomcat &amp; MySQL</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
